package com.itheima.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;

import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;

import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
//   @Resource
//   private DishFlavorService dishFlavorService;
////
    @Resource
    private CategoryService categoryService;
    @Resource
    private SetmealService setmealService;

    @Resource
    private DishService dishService;

   @Resource
    private DishFlavorService dishFlavorService;



//
    @PostMapping
    public R addsave (@RequestBody DishDto dishDto){
        boolean save = dishService.save(dishDto);
        return save?R.success("ok"):R.error("no");
    }


  //  接口四：分页查询菜品数据
    @GetMapping("/page")
    public R pageSelect( Integer page , Integer pageSize , String name) {
        IPage<Dish> dishPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(name !=null, Dish ::getName ,name);
        qw.orderByDesc(Dish::getUpdateTime);

        dishService.page(dishPage,qw);

        long total = dishPage.getTotal();
        List<Dish> records = dishPage.getRecords();

       Map  map = new HashMap<>();

        map.put("total",total);
        map.put("records",records);

        return R.success(map);

    }

//接口五：修改菜品数据
    @GetMapping("/{id}")
    public R<DishDto> updateSelect (@PathVariable Long id ){

        DishDto dishDto = dishService.getselectupdate(id);

        return R.success(dishDto);

}




//接口五：修改菜品数据
   @PutMapping
    public R update(@RequestBody DishDto dishDto) {

       int i = dishService.updateAdd(dishDto);

//       DishFlavor byId = dishFlavorService.getById(qw);
       return i>0?R.success("ok"):R.error("no");

    }






//    接口六：删除菜品接口[和批量删除用的是同一个接口]
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids ){

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish ::getId,ids);

        boolean remove = dishService.remove(qw);
        return remove? R.success("ok"):R.error("no");
    }

//接口七：起售/停售 批量起售/批量停售
    @PostMapping("/status/{status}")
    public R updateStatus (@PathVariable ("status") Integer status , @RequestParam List<Long> ids) {

        boolean b = false;
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            b = dishService.updateById(dish);
        }

        return b ? R.success("啊对对对") : R.error("戳了！极霸矛嘛");

    }



    @GetMapping("/list")
    public R<List<DishDto>> selectList( Long categoryId){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish ::getCategoryId,categoryId);
        List<Dish> list = dishService.list(qw);

        List<DishDto> collect = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);

            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> list1 = dishFlavorService.list(qw2);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(collect);
    }



}
